/*
 * Copyright (C) 2010 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package app.tellwmata.model;

import android.content.Context;

import app.tellwmata.Constants.FormFields;
import static app.tellwmata.Constants.FormFields.BUS_NUMBER;
import static app.tellwmata.Constants.FormFields.BUS_ROUTE_NUMBER;
import static app.tellwmata.Constants.FormFields.COMMENTS;
import static app.tellwmata.Constants.FormFields.RAIL_CAR_NUMBER;
import static app.tellwmata.Constants.FormFields.RAIL_LINE;
import static app.tellwmata.Constants.TYPE_BUS;

import app.tellwmata.R;

import java.text.MessageFormat;
import java.util.HashMap;

public final class MessageMaker {
    private static final String TAG = "MessageMaker";
    private final Context context;
    public static final String GOOD = "GOOD";
    public static final String HC = "HC";
    public static final String LATE = "LATE";
    public static final String DELAY = "DELAY";
    public static final String EARLY = "EARLY";
    public static final String NOSHOW = "NOSHOW";
    public static final String NOSRVSTOP = "NOSRVSTOP";
    public static final String DIRTY = "DIRTY";
    public static final String DOOR = "DOOR";
    public static final String DARK_P = "DARK_P";

    public MessageMaker(final Context context) {
        this.context = context;
    }

    public String getTwitterMessage(final String formType, int routeType, HashMap<String, String> form) {
        final StringBuilder msg = new StringBuilder();

        final int msgId;
        String field1 = RAIL_LINE;
        String field2 = RAIL_CAR_NUMBER;

        if (routeType == TYPE_BUS) {
            field1 = BUS_ROUTE_NUMBER;
            field2 = BUS_NUMBER;
        }

        if (GOOD.equals(formType)) {
            msgId = R.string.TW_GOOD;
        } else if (DELAY.equals(formType)) {
            msgId = R.string.TW_DELAY;
        } else if (HC.equals(formType)) {
            if (routeType == TYPE_BUS) {
                msgId = R.string.TW_HC_3;
            } else {
                msgId = R.string.TW_HC;
            }
        } else {
            msgId = -1;
            field1 = FormFields.FORM_TYPE;
        }

        if (msgId == -1) {
            msg.append(getTitle(formType, routeType));
        } else {
            msg.append(MessageFormat.format(context.getText(msgId).toString(), form.get(field1), form.get(field2)));
        }

        if (form.containsKey(COMMENTS)) {
            msg.append(" ").append(form.get(COMMENTS));
        }

        return msg.toString();
    }

    public String getRiderFormMessage(final String formType, final int routeType, final HashMap<String, String> form) {
        final StringBuilder msg = new StringBuilder();

        final int msgId;

        if (GOOD.equals(formType)) {
            msgId = R.string.F_GOOD;
        } else if (HC.equals(formType)) {
            if (routeType == TYPE_BUS) {
                msgId = R.string.F_HC_3;
            } else {
                msgId = R.string.F_HC;
            }
        } else if (LATE.equals(formType)) {
            msgId = R.string.F_LATE;
        } else if (DELAY.equals(formType)) {
            msgId = R.string.F_DELAY;
        } else if (EARLY.equals(formType)) {
            msgId = R.string.F_EARLY;
        } else if (NOSHOW.equals(formType)) {
            msgId = R.string.F_NOSHOW;
        } else if (NOSRVSTOP.equals(formType)) {
            msgId = R.string.F_NOSRVSTOP;
        } else if (DIRTY.equals(formType)) {
            msgId = R.string.F_DIRTY;
        } else if (DOOR.equals(formType)) {
            msgId = R.string.F_DOOR;
        } else if (DARK_P.equals(formType)) {
            msgId = R.string.F_DARK_P;
        } else {
            msgId = -1;
        }

        if (msgId == -1) {
            msg.append(getTitle(formType, routeType));
        } else {
            msg.append(context.getString(msgId));
        }

        if (form.containsKey(COMMENTS)) {
            msg.append("\n").append(form.get(COMMENTS));
        }

        return msg.toString();
    }

    public String getTitle(String formType, int routeType) {
        final String title;

        final int msgId;

        if (GOOD.equals(formType)) {
            msgId = R.string.T_GOOD;
        } else if (HC.equals(formType)) {
            if (routeType == TYPE_BUS) {
                msgId = R.string.T_HC_3;
            } else {
                msgId = R.string.T_HC;
            }
        } else if (LATE.equals(formType)) {
            msgId = R.string.T_LATE;
        } else if (DELAY.equals(formType)) {
            msgId = R.string.T_DELAY;
        } else if (EARLY.equals(formType)) {
            msgId = R.string.T_EARLY;
        } else if (NOSHOW.equals(formType)) {
            msgId = R.string.T_NOSHOW;
        } else if (NOSRVSTOP.equals(formType)) {
            msgId = R.string.T_NOSRVSTOP;
        } else if (DIRTY.equals(formType)) {
            msgId = R.string.T_DIRTY;
        } else if (DOOR.equals(formType)) {
            msgId = R.string.T_DOOR;
        } else if (DARK_P.equals(formType)) {
            msgId = R.string.T_DARK_P;
        } else {
            msgId = -1;
        }

        if (msgId == -1) {
            title = formType;
        } else {
            title = context.getString(msgId);
        }

        return title;
    }
}
